<template>
  <div class="alert-div">
    <div class="alert-fade" @click="closeFP"></div>
    <div class="alert-box" :class="{zzfapiao : fapiaoTab==1}">
      <div class="tit">{{fapiaoTab==3 ? '普通发票' : '增值发票' }}<i class="icon icon-close" @click="closeFP"></i></div>
      <div class="goods-list" v-show="fapiaoTab==3">
        <div class="item border-bottom">
          <label>发票抬头</label>
          <input class="flex-1" type="text" placeholder="请输入发票抬头" v-model="form1.invoiceTitle">
        </div>
        <div class="item border-bottom"><label>发票内容</label><b>食品</b></div>
      </div>
      <!--增值发票-->
      <div class="goods-list" v-show="fapiaoTab==1">
        <div class="item border-bottom">
          <label>发票抬头</label>
          <input class="flex-1" type="text" placeholder="请输入发票抬头" v-model="form.invoiceTitle"></div>
        <div class="item border-bottom">
          <label>纳税人识别码</label>
          <input class="flex-1" type="text" placeholder="请填写纳税人识别码" v-model="form.taxpayerCode">
        </div>
        <div class="item border-bottom">
          <label>注册地址</label>
          <input class="flex-1" type="text" placeholder="请填写注册地址"  v-model="form.regAddress">
        </div>
        <div class="item border-bottom">
          <label>注册电话</label>
          <input class="flex-1" type="text" placeholder="请填写注册电话"  v-model="form.regPhone">
        </div>
        <div class="item border-bottom">
          <label>开户银行</label>
          <input class="flex-1" type="text" placeholder="请填写开户银行"  v-model="form.bank">
        </div>
        <div class="item border-bottom">
          <label>银行账户</label>
          <input class="flex-1" type="text" placeholder="请填写银行账户"  v-model="form.bankAccount">
        </div>
        <div class="item border-bottom"><label>发票内容</label><b>明细</b></div>
      </div>
      <div class="alert-opt flex">
        <div class="btn3 flex-1"  @click="submitInvoice">完成</div>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    props: {
        fapiaoTab:''
    },
    data() {
      return {
        form: {//增值发票
          code: '',
          orderMainId:'',
          bank: '',
          bankAccount: '',
          invoiceContent: '明细',
          invoiceTitle: '',
          invoiceType: '1',//发票类型：1、增值税发票；2、个人；3、普票；4、电子发票
          orderCode: '',
          regAddress: '',
          regPhone: '',
          taxpayerCode: '',
        },
        form1: { //普通发票
            code: '',
            orderMainId:'',
            invoiceTitle: '',
            invoiceContent: '食品',
            invoiceType: '3',
        }
      };
    },
    methods: {
      closeFP(){
          this.$parent.isFapiao=false;
          this.$parent.fapiaoTab=0;
      },
      /**
       * 显示内容切换
       * @param <bool>
       * */
      showGeneral(bool){
        this.isGeneral = bool;
      },
      /**
       * 提交发票
       * */
      submitInvoice(){
        this.form.code=this.$parent.form.code;
        this.form.orderMainId=this.$parent.form.orderMainId;
        this.form1.code=this.$parent.form.code;
        this.form1.orderMainId=this.$parent.form.orderMainId;
        var subForm='';
        if(this.fapiaoTab==1){
            if(!this.verify()){
                return;
            }
            subForm=this.form;
        }else {
            if(!this.form1.invoiceTitle || this.form1.invoiceTitle == ''){
                Toast('发票抬头为空');
                return false;
            }
            subForm=this.form1;
        }
        setInvoice(subForm).then(data=>{
          if(data.returnCode == '000000'){
              Toast('发票信息设置成功');
              this.$parent.isFapiao=false;
              console.log(this.$parent.fapiaoTab);
          }
        })
      },
      verify(){
         if(!this.form.invoiceTitle || this.form.invoiceTitle == ''){
            Toast('发票抬头为空');
            return false;
         }
         if(!this.isGeneral && !this.form.taxpayerCode || this.form.taxpayerCode==''){
            Toast('纳税人识别码为空');
            return false;
         }
         if(!this.isGeneral && !this.form.regAddress  || this.form.regAddress==''){
            Toast('注册地址为空');
            return false;
         }
         if(!this.isGeneral && !this.form.regPhone || this.form.regPhone==''){
            Toast('注册电话为空');
            return false;
         }
         if(!this.isGeneral && !this.form.bank || this.form.bank==''){
            Toast('开户银行为空');
            return false;
         }
         if(!this.isGeneral && !this.form.bankAccount || this.form.bankAccount==''){
            Toast('银行账户为空');
            return false;
         }
         return true;
      },
      /**
       * 获取发票信息
       * */
      getInvoice(){
        getInvoice({code: this.orderCode}).then(data=>{
          if(data.returnCode == '000000'){
            this.form = data.result;
            this.form.orderCode = this.$route.params['orderCode'];
            [].forEach.call(data.result, (res)=>{
                if(res.invoiceType==1){
                    this.form =res;
                }else {
                    this.form1 =res;
                }
            });
          }
        })
      },
      /**
       * 是否开发票
       * @param <number> 1.开发票 2.不开发票
       * */
      setBill(type){
          this.isBill = type == 1;
          this.form.drawABill = type;
      }
    },
    filters: {},
    created() {
      this.orderCode = this.$route.params['orderCode'];
      this.getInvoice();
    },
    mounted(){

    },
    components: {
    }
  };
</script>
<style lang="less" scoped>
  .alert-div .alert-box.zzfapiao{
    height:10.41rem;
    .goods-list{
      height:8.55rem ;
    }
  }
</style>
